package com.example.shiro.controller;

import com.example.shiro.dto.AddUserDto;
import com.example.shiro.entity.User;
import com.example.shiro.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @ClassName UserController
 * @Description TODO 用户控制层
 * @Version 1.0
 */
@Controller
@RequestMapping("/user")
public class UserController {
    // 用户业务层
    @Autowired
    private UserService userService;

    /*
     * @Description //TODO 根据id查询用户数据
     * @Params [id]
     * @return com.example.shiro.entity.User
     * @version 1.0
     **/
    @GetMapping("/findById/{id}")
    public User findById(@PathVariable String id) {
        return userService.findById(id);
    }
    /*
     * @Description //TODO 根据id查询用户详情
     * @Params [id]
     * @return com.example.shiro.entity.User
     * @version 1.0
     **/
    @GetMapping("/getDetail/{id}")
    public User getDetail(@PathVariable String id) {
        return userService.getDetailById(id);
    }

    /*
     * @Description //TODO 新增用户
     * @Params [addUserDto]
     * @return java.util.Map<java.lang.String,java.lang.String>
     * @version 1.0
     **/
    @PostMapping("/add")
    public String add(@RequestParam String loginName,
                      @RequestParam String password,
                      @RequestParam String name) throws Exception {
        AddUserDto addUserDto = new AddUserDto();
        addUserDto.setLoginName(loginName);
        addUserDto.setName(name);
        addUserDto.setPassword(password);
        int res = userService.add(addUserDto);
        if (res == 1) {
            // 添加成功，跳转到登录页
            return "redirect:/login";
        } else {
            // 失败还是在新增用户页面
            return "redirect:/addUser";
        }
    }
}
